home *** CD-ROM | disk | FTP | other *** search
- Path: castle.nando.net!news
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.lang.c
- Subject: Re: Q: realloc->free?
- Date: 14 Jan 1996 22:28:04 GMT
- Organization: News & Observer Public Access
- Message-ID: <4dc01k$ifb@castle.nando.net>
- References: <4daa2e$oh5@axe.netdoor.com>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: grail801.nando.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4daa2e$oh5@axe.netdoor.com>, esargent@netdoor.com (Eric Sargent) writes:
- > This is probably a dumb question, but I can't find any specific or
- >exact information about this. Given this:
- >
- >char *a, *b;
- >
- >a = malloc(10);
- >....
- >/*
- > later we need to increase the size
- >*/
- >....
- >b = realloc(a, 100);
- >
- > Now let's say realloc had to move the data so a != b. Does realloc
- >free the memory previously pointed to by a or should it be explicitly
- >freed if realloc returns a new location? I checked the FAQ, but there
- >was nothing specific about realloc. Thanks for any information.
-
- If realloc() is successful, it handles any freeing needed. You may want
- to add, after the malloc() line:
-
- if ( b )
- a = NULL;
- else
- {
- free( a );
- fprintf( stderr, "Failure in realloc()\n" );
- exit( EXIT_FAILURE );
- }
-
- Don't ever use "a = realloc(a,n);" because of realloc() fails, you've lost
- the pointer to allocated memory.
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-
-